package com.hnkywl.sts.service.htdygl;

import com.hnkywl.sts.dao.htdygl.HtdyDao;
import com.hnkywl.sts.dao.htdygl.HtysdyDao;
import com.hnkywl.sts.entity.htgl.Htdy;
import com.hnkywl.sts.entity.htgl.Htys;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.Constant;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-4-11
 * Time: 上午11:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class HtdyService {

    @Autowired
    private HtdyDao htdyDao;

    @Autowired
    private HtysdyDao htysdyDao;

    public void setHtdyDao(HtdyDao htdyDao) {
        this.htdyDao = htdyDao;
    }

    public void setHtysdyDao(HtysdyDao htysdyDao) {
        this.htysdyDao = htysdyDao;
    }

    /**
     * 查询所有合同定义
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllHtdy(int pageNo, int pageSize) {
        Page<Htdy> htdyPage = htdyDao.findHtdyList(pageNo, pageSize);
        List<Htdy> htdyList = htdyPage.getResult() == null ? new ArrayList<Htdy>() : htdyPage.getResult();
        String htydJson = BaseUtils.fetchCollectionToJson(htdyList, "id", "nbgs.jc", "mc", "htlx", "bz");
        return "{\"total\":" + htdyPage.getTotalCount() + ",\"rows\":[" + htydJson + "]}";
    }

    /**
     * 查询所有合同定义,共合同元素使用
     *
     * @param htlx
     * @return
     */
    public String findAllHtdyByHtlx(String htlx) {
        String hql = "";
        if (StringUtils.isEmpty(htlx)) {
            hql = "from Htdy";
        } else {
            hql = "from Htdy where htlx = '" + htlx + "'";
        }
        List<Htdy> htdyList = this.htdyDao.find(hql);
        String htdyJson = BaseUtils.fetchCollectionToJson(htdyList, "id", "mc");
        return "[" + htdyJson + "]";
    }

    //关联判断
    private void nullHtdy(Htdy htdy) {
        if (htdy != null && htdy.getNbgs() != null && htdy.getNbgs().getId() == null)
            htdy.setNbgs(null);
    }

    /**
     * 保存合同定义以及合同元素
     *
     * @param htdy
     * @param htysArray
     * @param hId
     * @param htnrColumns
     */
    @Transactional
    public void saveHtdy(Htdy htdy, List<List> htysArray, String hId, List htnrColumns) {

        List<String> htysNameList = htysArray.get(0);
        List<String> htysTitleList = htysArray.get(1);
        //判断是修改还是新增，修改要删除合同元素的值
        if (StringUtils.isNotEmpty(hId)) {

            //删除合同元素
            deleteHtys(hId);

            // 保存合同定义
            nullHtdy(htdy);
            Htdy tempHtdy = this.htdyDao.get(htdy.getId());
            if (StringUtils.isNotEmpty(htdy.getBz())) {
                tempHtdy.setBz(htdy.getBz().trim());
            }
            tempHtdy.setMc(htdy.getMc().trim());
            tempHtdy.setNbgs(htdy.getNbgs());
            tempHtdy.setLastUpdated(new Date());
            tempHtdy.setHtml(htdy.getHtml());
            tempHtdy.setHtlx(htdy.getHtlx());
            // 赋值合同新元素
            List<Htys> htysList = new ArrayList<Htys>();
            for (int i = 0; i < htysNameList.size(); i++) {
                Htys tempHtys = new Htys();
                tempHtys.setMc(htysTitleList.get(i).toString());
                tempHtys.setYsid(htysNameList.get(i).toString());
                tempHtys.setHtdy(this.htdyDao.get(Long.valueOf(hId)));
                tempHtys.setDybzd(htnrColumns.get(i).toString().toLowerCase());
                tempHtys.setYxlx(Constant.XXLX_TEXT);
                tempHtys.setLastUpdated(new Date());
                htysList.add(tempHtys);
            }

            tempHtdy.setHtysList(htysList);

            this.htdyDao.save(tempHtdy);

        } else {
            // 保存合同定义
            htdy.setDateCreated(new Date());
            htdy.setLastUpdated(htdy.getDateCreated());
            htdy.setCreateUser(YgService.getCurrentYg());
            htdy.setCreateOrgan(YgService.getCurrentYg().getZggs());
            htdy.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            if (htdy.getNbgs() != null && htdy.getNbgs().getId() == null) {
                htdy.setNbgs(null);
            }
            this.htdyDao.save(htdy);
            // 保存合同元素
            for (int i = 0; i < htysNameList.size(); i++) {
                Htys htys = new Htys();
                htys.setDateCreated(new Date());
                htys.setLastUpdated(htys.getDateCreated());
                htys.setLastUpdated(htys.getLastUpdated());
                htys.setCreateUser(YgService.getCurrentYg());
                htys.setCreateOrgan(YgService.getCurrentYg().getZggs());
                htys.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                htys.setMc(htysTitleList.get(i).toString());
                htys.setYsid(htysNameList.get(i).toString());
                htys.setHtdy(htdy);
                htys.setYxlx(Constant.XXLX_TEXT);
                htys.setDybzd(htnrColumns.get(i).toString().toLowerCase());
                this.htysdyDao.save(htys);
            }
        }

    }

    /**
     * 修改合同定义
     *
     * @param htdy
     */
    @Transactional
    public void UpdateHtdy(Htdy htdy) {
        // 保存合同定义
        Htdy tempHtdy = this.htdyDao.get(htdy.getId());
        if (StringUtils.isNotEmpty(htdy.getBz())) {
            tempHtdy.setBz(htdy.getBz().trim());
        }
        nullHtdy(htdy);
        tempHtdy.setMc(htdy.getMc().trim());
        tempHtdy.setNbgs(htdy.getNbgs());
        tempHtdy.setHtlx(htdy.getHtlx());
        tempHtdy.setLastUpdated(new Date());
        this.htdyDao.save(tempHtdy);

    }

    /**
     * 根据id，查询合同定义实体
     *
     * @return
     */
    public Htdy findHtdyById(String htdyId) {
        if (StringUtils.isNotEmpty(htdyId)) {
            Htdy htdy = this.htdyDao.get(Long.valueOf(htdyId));
            return htdy;
        } else
            return null;
    }

    /**
     * 删除合同定义以及合同元素
     *
     * @param htdyId
     */
    @Transactional
    public void deleteHtdy(String htdyId) {
        //查询合同定义
        Htdy htdy = this.htdyDao.get(Long.valueOf(htdyId));
        if (null != htdy) {
            this.htdyDao.delete(htdy);
        }

    }

    /**
     * 删除合同元素
     *
     * @param htdyId
     */
    private void deleteHtys(String htdyId) {

        //查询合同定义对应的合同元素
        Htdy htdy = this.htdyDao.get(Long.valueOf(htdyId));
        if (null != htdy) {
            String delteteSql = "delete from t_biz_htys where htdy_id = ?";
            this.htysdyDao.executeSQL(delteteSql, htdy.getId());
        }
    }

    /**
     * 查询表字段
     *
     * @return
     */
    public List findAllColumns() {
        String sql = "select column_name from user_tab_columns where table_name = 'T_BIZ_HTNR'  and column_name != 'ID' ";
        return this.htdyDao.findBySQL(sql);
    }
}
